package com.yxx.community.controller;

import com.yxx.community.dto.NotificationDTO;
import com.yxx.community.enums.NotificationEnum;
import com.yxx.community.model.User;
import com.yxx.community.service.NotificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: YuXinXin
 * @Description:
 * @Date:Created in 21:46 2020/2/13
 **/
@Controller
public class NotificationController {
  @Autowired
  private NotificationService notificationService;

  @GetMapping("/notification/{id}")
  public String notification(@PathVariable("id")Long id,
                             HttpServletRequest request){
    User user = (User) request.getSession().getAttribute("user");
    if(user==null){
      return "redirect:/";
    }
    NotificationDTO notificationDTO=notificationService.read(id,user);
    if(NotificationEnum.REPLY_QUESTION.getType()==notificationDTO.getType()
            || NotificationEnum.REPLY_COMMENT.getType()==notificationDTO.getType()){
      return "redirect:/question/"+notificationDTO.getOuterId();
    }else {
      return "redirect:/";
    }
  }
}
